import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/ui/custom/LoadingDialog.dart';
import 'package:flutter_juwo/ui/utils/progress_view.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';

class ShowPicPage extends StatefulWidget {
  final String picUrl;

  ShowPicPage(this.picUrl);

  @override
  _ShowPicPageState createState() => _ShowPicPageState();
}

class _ShowPicPageState extends State<ShowPicPage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        children: <Widget>[
          GestureDetector(
            onTap: () {
              print("点击了图片");
              showDialog(
                  context: context,
                  barrierDismissible: false,
                  builder: (BuildContext context) {
                    return new LoadingDialog(
                      text: "保存中…",
                    );
                  });
              _saved(widget.picUrl, context);
            },
            child: Container(
                margin: EdgeInsets.only(
                    left: ScreenUtil().setWidth(100),
                    right: ScreenUtil().setWidth(100)),
                padding: EdgeInsets.only(
                    left: ScreenUtil().setWidth(20),
                    right: ScreenUtil().setWidth(20),
                    top: ScreenUtil().setWidth(40),
                    bottom: ScreenUtil().setWidth(60)),
                child: CachedNetworkImage(
                  width: ScreenUtil().setWidth(690),
                  fit: BoxFit.contain,
                  imageUrl: widget.picUrl,
                  placeholder: (context, url) => new ProgressView(),
                  errorWidget: (context, url, error) => new Icon(
                    Icons.error,
                    color: Colors.redAccent,
                  ),
                )),
          ),
          Positioned(
            right: ScreenUtil().setWidth(30),
            top: ScreenUtil().setWidth(0),
            child: GestureDetector(
              onTap: () {
                NavigatorUtils.goBack(context);
              },
              child: Container(
                child: Image.asset(
                  Utils.getImgPath("icon_cancel"),
                  width: ScreenUtil().setWidth(60),
                ),
              ),
            ),
          ),
          Positioned(
            bottom: 0,
            child: Container(
              width: ScreenUtil().setWidth(750),
              alignment: Alignment.center,
              child: Text(
                "点击保存推广海报",
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(30),
                    color: Colors.white,
                    fontFamily: Font.PingFangMedium,
                    decoration: TextDecoration.none),
              ),
            ),
          )
        ],
      ),
    );
  }

  _saved(String imgUrl, BuildContext context) async {
    var response = await Dio()
        .get(imgUrl, options: Options(responseType: ResponseType.bytes));
    final result =
        await ImageGallerySaver.save(Uint8List.fromList(response.data));
    Navigator.pop(context);
    if (result) {
      ToastUtil.showShortToast("保持成功,请在相册中查看");
    } else {
      ToastUtil.showShortErrToast("保持失败，请稍后重试");
    }
  }
}
